using AutoMapper;
using hao_Model;
using hao_Dto.User;

namespace hao_Common.AutoMapper.Profiles
{
    /// <summary>
    /// 地址映射配置
    /// </summary>
    public class AddressProfile : Profile
    {
        public AddressProfile()
        {
            // Address -> AddressDto
            CreateMap<Address, AddressDto>()
                .ForMember(dest => dest.FullAddress, 
                          opt => opt.MapFrom(src => $"{src.Province}{src.City}{src.District}{src.DetailAddress}"));

            // CreateAddressDto -> Address
            CreateMap<CreateAddressDto, Address>()
                .ForMember(dest => dest.Id, opt => opt.Ignore())
                .ForMember(dest => dest.UserId, opt => opt.Ignore())
                .ForMember(dest => dest.Status, opt => opt.MapFrom(src => 1))
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateTime, opt => opt.Ignore());

            // UpdateAddressDto -> Address (用于更新时的映射)
            CreateMap<UpdateAddressDto, Address>()
                .ForMember(dest => dest.Id, opt => opt.Ignore())
                .ForMember(dest => dest.UserId, opt => opt.Ignore())
                .ForMember(dest => dest.Status, opt => opt.Ignore())
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateTime, opt => opt.Ignore())
                .ForAllMembers(opt => opt.Condition((src, dest, srcMember) => srcMember != null));

            // ImportAddressDto -> CreateAddressDto
            CreateMap<ImportAddressDto, CreateAddressDto>()
                .ForMember(dest => dest.ReceiverName, opt => opt.MapFrom(src => src.ReceiverName))
                .ForMember(dest => dest.ReceiverPhone, opt => opt.MapFrom(src => src.ReceiverPhone))
                .ForMember(dest => dest.Province, opt => opt.MapFrom(src => src.Province))
                .ForMember(dest => dest.City, opt => opt.MapFrom(src => src.City))
                .ForMember(dest => dest.District, opt => opt.MapFrom(src => src.District))
                .ForMember(dest => dest.DetailAddress, opt => opt.MapFrom(src => src.DetailAddress))
                .ForMember(dest => dest.PostalCode, opt => opt.MapFrom(src => src.PostalCode))
                .ForMember(dest => dest.AddressLabel, opt => opt.MapFrom(src => src.AddressLabel))
                .ForMember(dest => dest.IsDefault, opt => opt.MapFrom(src => src.IsDefault))
                .ForMember(dest => dest.Longitude, opt => opt.Ignore())
                .ForMember(dest => dest.Latitude, opt => opt.Ignore());

            // AddressDto -> Address (用于导出等场景)
            CreateMap<AddressDto, Address>()
                .ReverseMap();
        }
    }
}